package com.kerrykidz.system.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kerrykidz.common.Constants;
import com.kerrykidz.system.service.IUserService;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.util.security.PrincipalInfo;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: UserController
 * @description: 系统相关控制器
 * @author: zhuss
 * @date: 2014年9月4日 下午2:56:51
 * @version: V1.0
 * 
 */
@Controller
@RequestMapping("/system/user")
public class UserController {

	@Autowired
	private IUserService userService;

	@RequestMapping(value = "/logonSuccess")
	public String loginRedirect(Integer loginStatus, ModelMap model) {
		if (loginStatus != null) {
			model.put("loginStatus", loginStatus);
			return "login";
		} else {
			PrincipalInfo info = PrincipalInfo.getInstance();
			model.put("meuns", info.getMenus());
			model.put("loginBran", info.getBranId());
			if(null != info.getRoleList()){
				//如果是门店经理、前台或检票员,启动进场人数预警定时器
				if(info.getRoleList().contains("103")||info.getRoleList().contains("104")||info.getRoleList().contains("105"))
					model.put("runFloor", "1");
				//如果是门店经理或仓库管理员,启动库存预警定时器
				if(info.getRoleList().contains("103")||info.getRoleList().contains("111"))
					model.put("runScm", "1");
			}
			return "mainIndex";
		}
	}

	// 登陆成功pad跳转
	@RequestMapping(value = "/padLogonSuccess")
	public String padLoginRedirect(Integer loginStatus, ModelMap model) {
		if (loginStatus != null) {
			model.put("loginStatus", loginStatus);
			return "padLogin";
		} else {
			PrincipalInfo info = PrincipalInfo.getInstance();
			model.put("meuns", info.getMenus());
			return "redirect:/system/index.shtml?mode=pad";
		}
	}

	@RequestMapping("/checkPwd")
	@ResponseBody
	public String checkPwd(String oldpassword) throws RDPException {
		UserInfo userInfo = UserUtil.getUserFromSession();
		String oldPwd = userService.findEntityById(userInfo.getUserId()).getLoginPwd();
		return oldPwd.equals(oldpassword) ? "success" : "failure";
	}

	@RequestMapping("/updatePwd")
	@ResponseBody
	public String updatePwd(@RequestParam("loginPwd") String loginPwd,
			RedirectAttributes redirectAttributes) throws RDPException {
		String userId = UserUtil.getUserId();
		if (StringUtils.isNotBlank(userId)) {
			userService.updatePwd(loginPwd, userId);
		}
		return Constants.SUCCESS;
	}
	/**
	 * 修改密码for pad
	 * @author ScreenJxp
	 * @param loginPwd
	 * @param oldpassword
	 * @param redirectAttributes
	 * @return
	 * @throws RDPException
	 * @date: 2014年11月12日
	 */
	@RequestMapping("/updatePwdForPad")
	@ResponseBody
	public String updatePwdForPad(@RequestParam("loginPwd") String loginPwd,@RequestParam("oldpassword")String oldpassword,
			RedirectAttributes redirectAttributes) throws RDPException {
		String userId = UserUtil.getUserId();
		UserInfo userInfo = UserUtil.getUserFromSession();
		String oldPwd = userService.findEntityById(userInfo.getUserId()).getLoginPwd();
		if(oldPwd!=null){
			if(oldPwd.equals(oldpassword)){
				userService.updatePwd(loginPwd, userId);
			}else{
				return Constants.FAILURE;
			}
		}else{
			return Constants.FAILURE;
		}
		return Constants.SUCCESS;
	}

	@RequestMapping(value = "/logout")
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "login";
	}

	@RequestMapping("/updatePass")
	public String up() {
		return "indexPage/update_pass";
	}

	/**
	 * 修改密码for pad
	 * 
	 * @return
	 * @throws RDPException
	 * @date: 2014年10月15日
	 */
	@RequestMapping("/updatePadPass")
	public ModelAndView update() {
		ModelAndView mav=new ModelAndView("pad/ipad_logout/logout");
		mav.addObject("user", UserUtil.getUserFromSession());
		return mav;
	}
}
